<?php
/**
 * View properties of the rail_object in $_GET['object]
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-08-06
 * @package rail
 */

session_start();

require_once( 'railbrowser.conf.php' );
require_once( $cfg_pahToPHPrail . '/rail_connector.class.php' );
require_once( 'includes/derive_icon.php' );

$rail = new rail_connector( $cfg_mysql_db, $cfg_mysql_user, $cfg_mysql_passwd, $cfg_mysql_host );
$rail->login($cfg_rail_user, $cfg_rail_passwd);

$object = rail_factory::get_object( $rail, $_REQUEST['object'] );

$smarty = new rail_smarty();
$smarty->caching = false;


/* ------------------------------------ Listener -------------------------------------- */
if( isset($_POST['group']) ){
	// set user
	if( isset($_POST['user']) &&  $_POST['user'] != $object->get_user()->get_id() )
		$object->set_user( rail_factory::get_object( $rail, $_POST['user'] ) );
	// set group
	if( $_POST['group'] != $object->get_group()->get_id() )
		$object->set_group( rail_factory::get_object( $rail, $_POST['group'] ) );
	// chmod
	$mode = $_POST['rights_user'] . $_POST['rights_group'] . $_POST['rights_other'];
	if( $mode != $object->get_mode() )
		$object->set_mode( $mode );

	header('Location: properties.php?object=' . $object->get_id() );
}

/* --------------------------------- Current object ----------------------------------- */
// object data
$smarty->assign( array(
	'object' => array(
		'is_readable' => $object->is_readable( $rail->get_user() ),
		'is_writeable' => $object->is_writeable( $rail->get_user() ),
		'is_rootuser' => $rail->get_user()->get_name() == "root",
		'is_currentuser_user' => $rail->get_user()->get_id() == $object->get_user()->get_id(),
		'type' => get_class( $object ),
		'id' => $object->get_id(),
		'name' => $object->get_name(),
		'path' => $object->get_path(),
		'user' => $object->get_user()->get_id(),
		'group' => $object->get_group()->get_id(),
		'mode' => array(
			'user' => substr( $object->get_mode(), 0, 1 ),
			'group' => substr( $object->get_mode(), 1, 1 ),
			'other' => substr( $object->get_mode(), 2, 1 )
		),
		'time_create' => $object->is_readable( $rail->get_user() ) ? date( 'D d.m.Y H:i', $object->get_attribute( OBJ_TIME_CREATE ) ) : '--',
		'time_lastwrite' => $object->is_readable( $rail->get_user() ) ? date( 'D d.m.Y H:i', $object->get_attribute( OBJ_TIME_LASTWRITE ) ) : '--',
		'icon' => derive_icon( $rail, $object ),
	)
));


/* --------------------------------------- Users -------------------------------------- */
$rail_users = array();
$data = $rail->send_request( "SELECT `id` FROM `objects` WHERE `type`='RAIL_USER'" );
while( $data_user = $data->fetch_assoc() ){
	$user = rail_factory::get_object( $rail, $data_user['id'] );
	$rail_users[] = array(
		'id' => $user->get_id(),
		'name' => $user->get_name()
	);
}
$smarty->assign( 'rail_users', $rail_users );


/* --------------------------------------- Groups ------------------------------------- */
// get all groups
$rail_groups = array();
$data = $rail->send_request( "SELECT `id` FROM `objects` WHERE `type`='RAIL_GROUP'" );
while( $data_group = $data->fetch_assoc() ){
	$group = rail_factory::get_object( $rail, $data_group['id'] );
	// check wheater rail user is root user or current user are member of this group
	$group_ids = array();
	foreach ( $rail->get_user()->get_memberships() as $memberships ) $group_ids[] = $memberships->get_id();
	if( !(!in_array($group->get_id(), $group_ids) && $rail->get_user()->get_id() == $object->get_user()->get_id()) || $rail->get_user()->get_name() == "root" ){
		$rail_groups[] = array(
			'id' => $group->get_id(),
			'name' => $group->get_name()
		);
	}
}
$smarty->assign( 'rail_groups', $rail_groups );


/* ------------------------------------- Relations ------------------------------------ */
if( $object->is_readable( $rail->get_user() ) ){
	$relations = array();
	foreach( $object->get_relation_types() as $relation ){
		$tmp = array();
		foreach( $object->get_relations( $relation ) as $obj )
			$tmp[] = array(
				'id' => $obj->get_id(),
				'path' => $obj->get_path(),
				'icon' => derive_icon( $rail, $obj )
			);

		$relations[] = array(
			'type' => $relation,
			'objects' => $tmp,
		);
	}
	$smarty->assign( array(
		'relations' => $relations,
		'relations_size' => count( $relations )
	));
}


/* -------------------------------------- Display ------------------------------------- */
$smarty->display( 'properties.tpl' );

?>